<?php
/**
 * 배추통합검색  (UnitedSearch for Gnuboard4)
 *
 * Copyright (c) 2010 Choi Jae-Young <www.miwit.com>
 *
 * 저작권 안내
 * - 저작권자는 이 프로그램을 사용하므로서 발생하는 모든 문제에 대하여 책임을 지지 않습니다. 
 * - 이 프로그램을 어떠한 형태로든 재배포 및 공개하는 것을 허락하지 않습니다.
 * - 이 저작권 표시사항을 저작권자를 제외한 그 누구도 수정할 수 없습니다.
 */

include_once("_common.php");
include_once("_lib.php");

if ($is_admin != "super")
    alert_close("접근권한이 없습니다.");

$g4[title] = "배추 통합검색 관리자";
include_once("$g4[path]/head.sub.php");

$groups_list = array();
$reject_list = array();
$search_list = array();

$us_group = explode(",", trim($mwus[us_group]));

for ($i=0, $m=sizeof($us_group); $i<$m; $i++) {
    if ($us_group[$i] == "@site")
        $search_list[] = $mwus_site;
    else
        $search_list[] = sql_fetch("select gr_id, gr_subject from $g4[group_table] where gr_id = '{$us_group[$i]}'");
}

$sql = "select gr_id, gr_subject from $g4[group_table]";
$qry = sql_query($sql);
for ($i=0; $row=sql_fetch_array($qry); $i++) {
    $groups_list[] = $row;

    if (!in_array($row[gr_id], $us_group))
        $reject_list[] = $row;
}

if (!in_array("@site사이트", @array_map("implode",$search_list)) && !in_array("@site", @array_map("implode",$reject_list)))
    $reject_list[] = array("gr_id" => "@site", "gr_subject" => "사이트");

$search_list = array_map("unserialize", array_unique(array_map("serialize", $search_list)));
$reject_list = array_map("unserialize", array_unique(array_map("serialize", $reject_list)));

$row = sql_fetch("select sum(bo_count_write) as total_write, sum(bo_count_comment) as total_comment from $g4[board_table]");
$total_write = $row[total_write];
$total_comment = $row[total_comment];

$row = sql_fetch("select count(*) as cnt from $mwus[search_table] where wr_is_comment = 0");
$search_write = $row[cnt];

$row = sql_fetch("select count(*) as cnt from $mwus[search_table] where wr_is_comment = 1");
$search_comment = $row[cnt];

sql_query("alter table $mwus[config_table] add us_sop varchar(5) default 'and' not null", false);
?>

<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="js/selectbox.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("#tabs").tabs();
});

function us_config_submit() {
    $.post("admin_update.php", { 'w':'u', 'us_skin':$("#us_skin").val(), 'us_sfl':$("#us_sfl").val(), 'us_sop':$("#us_sop").val() }, function (req) {
        if (req == "true")
            alert("적용되었습니다.");    
        else
            alert("실패하였습니다.");
    });
}

function us_group_submit() {
    var us_group = "";
    $("#us_search").selectAllOptions();    
    $("#us_search :selected").each(function (i, selected) {
        us_group += "," + $(selected).val();
    });
    $.post("admin_update.php", { 'w':'g', 'us_group':us_group }, function (req) {
        if (req == "true")
            alert("적용되었습니다.");    
        else
            alert("실패하였습니다.");
    });
}

function us_group_left() {
    $("#us_reject :selected").remove().appendTo("#us_search");
}

function us_group_right() {
    $("#us_search :selected").remove().appendTo("#us_reject");
}

function us_group_up() {
    $("#us_search").moveOptionUp();
}

function us_group_down() {
    $("#us_search").moveOptionDown();
}

function us_sync() {

    if (!confirm("동기화를 시작하시겠습니까?")) return false;

    var btn = $("#bt_sync").html();

    $("#bt_sync").html("<img src=\"img/icon_loading.gif\">");

    $.post("admin_update.php", { 'w':'s' }, function (req) {
        alert("동기화 되었습니다.");
        $("#bt_sync").html(btn);
    });
}
</script>

<link rel="stylesheet" href="js/ui-lightness/jquery-ui-1.7.2.custom.css" type="text/css"/>
<style type="text/css">
#us_admin { margin:20px; }
#us_admin h3 { }
#us_admin .item { height:30px; font-size:12px; font-weight:bold; }
#us_admin .item .item_label { float:left; width:80px; }
#us_admin .item .item_value { float:left; }
#us_admin .bt { background-color:#efefef; cursor:pointer; }
#us_admin .group { float:left; width:200px; }
#us_admin .group .subject { float:left; width:200px; font-size:12px; font-weight:bold; height:20px; }
#us_admin .group .selectbox { width:200px; height:150px; font-size:12px; padding:5px; }
#us_admin .group .button_change { text-align:center; padding:10px 0 0 0; }
#us_admin .button_move { float:left; padding:55px 10px 0 10px;  }
#us_admin .button_submit { clear:both; width:470px; padding:20px 0 0 0; text-align:center; }
#us_admin .bt { background-color:#efefef; font-size:12px; font-family:dotum; }
#us_admin .block { clear:both; height:20px; }
#us_admin .info { height:30px; font-size:12px; }
</style>

<div id="us_admin">

<h3>배추 통합검색 관리자</h3>

<div id="tabs">
<ul>
    <li> <a href="#tabs-1">기본설정</a> </li>
    <li> <a href="#tabs-2">그룹관리</a> </li>
    <li> <a href="#tabs-3">동기화</a> </li>
    <li> <a href="#tabs-4">사이트 관리</a> </li>
    <li> <a href="#tabs-5">바로가기 관리</a> </li>
</ul>

<div id="tabs-1" class="tabs"> <!-- 기본설정 -->

    <form name="fconfig" method="post" action="admin_update.php">
    <input type="hidden" name="w" value="u"/>

    <div class="item">
        <div class="item_label"> 스킨선택 </div>
        <div class="item_value">
            <select name="us_skin" id="us_skin" required itemname="통합검색 스킨">
            <option value=""></option>
            <?
            $skin_dir = mwus_skin_dir();
            for ($i=0, $m=count($skin_dir); $i<$m; $i++) {
                echo "<option value=\"{$skin_dir[$i]}\">{$skin_dir[$i]}</option>\n";
            }
            ?>
            </select>
            <script type="text/javascript"> fconfig.us_skin.value = "<?=$mwus[us_skin]?>"; </script>
        </div>
    </div>

    <div class="item">
        <div class="item_label"> 검색범위 </div>
        <div class="item_value">
            <select name="us_sfl" id="us_sfl" required itemname="검색범위">
            <option value=""></option>
            <option value="wr_subject">제목</option>
            <option value="wr_content">내용</option>
            <option value="wr_subject,wr_content">제목,내용</option>
            </select>
            <script type="text/javascript"> fconfig.us_sfl.value = "<?=$mwus[us_sfl]?>"; </script>
        </div>
    </div>

    <div class="item">
        <div class="item_label"> 검색조건 </div>
        <div class="item_value">
            <select name="us_sop" id="us_sop" required itemname="검색조건">
            <option value=""></option>
            <option value="and">AND</option>
            <option value="or">OR</option>
            </select>
            <script type="text/javascript"> fconfig.us_sop.value = "<?=$mwus[us_sop]?>"; </script>
        </div>
    </div>

    <div class="button_submit">
        <input type=button class="bt" value="적     용" onclick="us_config_submit()">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type=button class="bt" value="닫     기" onclick="self.close();">
    </div>
    </form>

</div> <!-- 기본설정 -->


<div id="tabs-2" class="tabs"> <!-- 그룹관리 -->

    <div class="group">

        <div class="subject"> ◆ 검색 그룹 </div>
        <select name="us_search" id="us_search" multiple class="selectbox">
        <? for ($i=0, $m=sizeof($search_list); $i<$m; $i++) { ?>
        <? if (trim($search_list[$i][gr_id]) == "") continue; ?>
        <option value="<?=$search_list[$i][gr_id]?>"> <?=$search_list[$i][gr_subject]?> </option>
        <? } ?>
        </select>
        <div class="button_change">
            <input type="button" class="bt" value="↑" onclick="us_group_up()">
            <input type="button" class="bt" value="↓" onclick="us_group_down()">
        </div>
    </div>

    <div class="button_move">
        <input type="button" class="bt" value="《" onclick="us_group_left()">
        <input type="button" class="bt" value="》" onclick="us_group_right()">
    </div>

    <div class="group">
        <div class="subject"> ◆ 제외 그룹 </div>
        <select name="us_reject" id="us_reject" multiple class="selectbox">
        <? for ($i=0, $m=sizeof($reject_list); $i<$m; $i++) { ?>
        <option value="<?=$reject_list[$i][gr_id]?>"> <?=$reject_list[$i][gr_subject]?> </option>
        <? } ?>
        </select>
    </div>

    <div class="button_submit">
        <input type=button class="bt" value="적     용" onclick="us_group_submit()">
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type=button class="bt" value="닫     기" onclick="self.close();">
    </div>

</div> <!-- 그룹관리 -->

<div id="tabs-3" class="tabs"> <!-- 동기화 -->

    <div class="info"> 사이트내 게시판DB 와 검색DB 를 동기화 합니다. </div>
    <div class="info"> 사이트DB 게시물 수 : <?=number_format($total_write)?> 개, 댓글 수 : <?=number_format($total_comment)?> 개 </div>
    <div class="info"> 검색DB 게시물 수 : <?=number_format($search_write)?> 개, 댓글 수 : <?=number_format($search_comment)?> 개 </div>
    <div id="bt_sync"> <input type="button" value="동기화 시작" class="bt" onclick="us_sync()"> </div>

</div> <!-- 동기화 -->


<div id="tabs-4" class="tabs"> <!-- 사이트 관리 -->

    <iframe width="720" height="300" style="margin:0 0 10px 0; border:1px solid #ccc;" name="isite" src="admin-site.php"></iframe>

</div> <!-- 사이트 관리 -->

<div id="tabs-5" class="tabs"> <!-- 바로가기 관리 -->

    <iframe width="720" height="300" style="margin:0 0 10px 0; border:1px solid #ccc;" name="idirect" src="admin-direct.php"></iframe>

</div> <!-- 바로가기 관리 -->

<div class="block"></div>

</div> <!-- tabs -->

</div> <!-- us_admin -->

<?
include_once("$g4[path]/tail.sub.php");
